package com.xmall.pethouse_api.controller;

import com.xmall.pethouse_api.pojo.Result;
import com.xmall.pethouse_api.pojo.entity.ShopProduct;
import com.xmall.pethouse_api.pojo.entity.ShopCart;
import com.xmall.pethouse_api.pojo.entity.ShopOrder;
import com.xmall.pethouse_api.pojo.entity.ShopOrderItem;
import com.xmall.pethouse_api.service.ShopService;
import com.xmall.pethouse_api.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;

    // 商品相关
    @GetMapping("/products/list")
    public Result<List<ShopProduct>> listProducts() {
        return Result.success(shopService.listProducts());
    }
    @GetMapping("/product/byId")
    public Result<ShopProduct> getProduct(@RequestParam Long ProductId) {
        return Result.success(shopService.getProductById(ProductId));
    }

    // 购物车相关
    @GetMapping("/cart")
    public Result<List<com.xmall.pethouse_api.pojo.vo.CartVO>> listCart() {
        Long userId = getUserId();
        return Result.success(shopService.listCartVOByUserId(userId));
    }
    @PostMapping("/cart/add")
    public Result addCartItem(@RequestParam Long productId, @RequestParam Integer quantity) {
        Long userId = getUserId();
        shopService.addCartItem(userId, productId, quantity);
        return Result.success();
    }
    @PostMapping("/cart/update")
    public Result updateCartItem(@RequestParam Long cartId, @RequestParam Integer quantity) {
        shopService.updateCartItem(cartId, quantity);
        return Result.success();
    }
    @PostMapping("/cart/delete")
    public Result deleteCartItem(@RequestParam Long cartId) {
        shopService.deleteCartItem(cartId);
        return Result.success();
    }
    @PostMapping("/cart/clear")
    public Result clearCart() {
        Long userId = getUserId();
        shopService.clearCart(userId);
        return Result.success();
    }

    // 订单相关
//    @GetMapping("/orders/list")
//    public Result<List<ShopOrder>> listOrders() {
//        Long userId = getUserId();
//        return Result.success(shopService.listOrdersByUserId(userId));
//    }
    @GetMapping("/order/list")
    public Result<ShopOrder> getOrder() {
        Long userId = getUserId();
        return Result.success(shopService.getOrderById(userId));
    }
    @PostMapping("/order/create")
    public Result createOrder(@RequestParam Long addressId, @RequestParam(required = false) String remark) {
        Long userId = getUserId();
        shopService.createOrder(userId, addressId, remark);
        return Result.success();
    }
    @GetMapping("/order/items/{orderId}")
    public Result<List<ShopOrderItem>> listOrderItems(@PathVariable Long orderId) {
        return Result.success(shopService.listOrderItemsByOrderId(orderId));
    }

    private Long getUserId() {
        Map<String, Object> userMap = ThreadLocalUtil.get();
        return Long.valueOf(userMap.get("id").toString());
    }
} 